package com.cmwa.solution.business.demand.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Employee;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.demand.dao.PrdDemandDao;
import com.cmwa.solution.business.demand.entity.PrdDemandVo;
import com.cmwa.solution.business.demand.service.PrdDemandService;
import com.cmwa.solution.sys.attach.service.AttachService;
import com.cmwa.solution.utils.SessionUtils;

@Service
public class PrdDemandServiceImpl implements PrdDemandService {

	@Resource
	private PrdDemandDao prdDemandDao;
	
	@Resource
	private AttachService attachService;
	
	@Override
	public Page<PrdDemandVo> prdDemandListPage(SearchParam sp) {
		Employee employee = SessionUtils.getEmployee();
		
		if (StringUtils.equals(employee.getEmpType(), "OUT")) {
			sp.getSp().put("company", employee.getCompany());
		}
		List<PrdDemandVo> items = prdDemandDao.prdDemandListPage(sp);
		return Page.create(items, sp.getStart(), sp.getLimit(), sp.getTotal());
	}

	@Override
	public void saveDemand(PrdDemandVo demandVo, String attIds) {
		Employee employee = SessionUtils.getEmployee();
		demandVo.setCreateId(employee.getID());
		prdDemandDao.insertDemand(demandVo);
		if (StringUtils.isNotEmpty(attIds)) {
			attachService.addAttachRel2(employee.getID(), demandVo.getDemandId(), "DEMAND_INFO", attIds);
		}
	}

	@Override
	public PrdDemandVo demandDetail(String demandId) {
		return prdDemandDao.demandDetail(demandId);
	}

	@Override
	public void updateDemand(PrdDemandVo demandVo, String attIds) {
		Employee employee = SessionUtils.getEmployee();
		demandVo.setModifyId(employee.getID());
		prdDemandDao.updateDemand(demandVo);
		attachService.deleteAttachRel2(demandVo.getDemandId(), "DEMAND_INFO");
		if (StringUtils.isNotEmpty(attIds)) {
			attachService.addAttachRel2(employee.getID(), demandVo.getDemandId(), "DEMAND_INFO", attIds);
		}
	}

	@Override
	public void deleteDemandById(String demandId) {
		prdDemandDao.deleteDemandById(demandId);
	}

}
